package com.liu;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.annotation.Resources;

/**
 * RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式，
 *      常见的http客户端请求工具：JDK HttpURLConnection（默认底层执行引擎） 、 Apache HttpClient 和 OKHttp
 *
 * 在 spring 发展过程中，在 spring5 里，spring 团队提出并推介了一个新的 Http 客户端: webClient,
 * 并说明 RestTemplate 将在未来的版本中弃用，并且未来不会添加主要的新功能
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }


    @Resource(name = "httpRestTemplate")
    private RestTemplate restTemplate;

    @PostConstruct
    public void queryWeather() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        final ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
        if (requestFactory instanceof HttpComponentsClientHttpRequestFactory){
            final HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) requestFactory;
            final HttpClient httpClient = factory.getHttpClient();

            final ClientConnectionManager connectionManager = httpClient.getConnectionManager();
        }


        ResponseEntity<Object> forEntity = restTemplate.getForEntity("https://restapi.amap.com/v3/weather/weatherInfo?city=510100&key=e7a5fa943f706602033b6b329c49fbc6", Object.class);
        System.out.println("状态码:"+forEntity.getStatusCode());
        System.out.println("状态码内容:"+forEntity.getStatusCodeValue());
        HttpHeaders headers = forEntity.getHeaders();
        System.out.println("响应头:"+headers);
        Object body = forEntity.getBody();
        System.out.println("响应内容:"+body);
    }

}
